<?php
namespace app\index\controller;

use app\common\controller\Frontend;
use think\Config;
use think\Cookie;
use think\Hook;
use think\Session;
use think\Validate;


/**
 * 每日算力
 */
class UserMoneyLog extends Frontend
{

    protected $layout = 'default';
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $auth = $this->auth;

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 每日算力
     */
    public function index()
    {
        /*$map = [
            'log1.user_id' => $this->auth->id,
            'log1.type' => 1,

            'log2.user_id' => $this->auth->id,
            'log2.type' => 2,

            'log3.user_id' => $this->auth->id,
            'log3.type' => 3,
        ];
        $logList = db('UserMoneyLog log1')->where($map)->join('UserMoneyLog log2', 'log1.date=log2.date')->join('UserMoneyLog log3', 'log1.date=log3.date')->order('log1.createtime', 'desc')->field('log1.money static_money,log1.date, log2.money dynamic_money, log3.money share_money')->select();*/

        $map = [
            'user_id' => $this->auth->id,
        ];
        $log2 = db('UserMoneyLog')->where($map)->where('type', 2)->field('money, date')->buildSql();
        $log3 = db('UserMoneyLog')->where($map)->where('type', 3)->field('money, date')->buildSql();

        $map = [
            'log1.user_id' => $this->auth->id,
            'log1.type' => 1,
        ];
        $logList = db('UserMoneyLog log1')->where($map)->join([$log2 => 'log2'], 'log1.date=log2.date', 'LEFT')->join([$log3 => 'log3'], 'log1.date=log3.date', 'LEFT')->order('log1.createtime', 'desc')->field('log1.money static_money, log1.date, log2.money dynamic_money, log3.money share_money')->group('date')->select();

        foreach ($logList as $k => &$v) {
            $v['static_money'] = money_format_new($v['static_money']);//静态
            $v['dynamic_money'] = money_format_new($v['dynamic_money']);//分享
            $v['share_money'] = money_format_new($v['share_money']);//共享
            $v['date'] = datetime(strtotime($v['date']), 'Y-m-d');
        }
        unset($v);

        //dump($logList);
  

        $this->view->assign('logList', $logList);

        $this->view->assign('title', '每日算力');
        return $this->view->fetch();
    }

}
